package com.qen.jiaoyu.tangshi

import cn.hutool.core.lang.Console
import cn.hutool.core.util.CharsetUtil
import cn.hutool.poi.excel.ExcelUtil
import com.qen.epub.removeNoZhCn
import it.skrape.core.htmlDocument
import it.skrape.fetcher.BrowserFetcher
import it.skrape.fetcher.Method
import it.skrape.fetcher.response
import it.skrape.fetcher.skrape
import nl.siegmann.epublib.domain.Author
import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.Resource
import nl.siegmann.epublib.domain.Resources
import nl.siegmann.epublib.epub.EpubWriter
import nl.siegmann.epublib.service.MediatypeService
import java.io.File
import java.io.FileOutputStream
import kotlin.collections.forEach

fun main() {

    val reader = ExcelUtil.getReader("$TangShiExcelPath/唐诗三百首.xlsx")
    val readAll = reader.read(2)

    var resources: Resources = Resources()
    var book: Book = Book()
    book.metadata.addTitle("唐诗三百首")
    book.metadata.addAuthor(Author("MayBeTheGo"))
//    for (i in 0..1) {
//        val list = readAll.get(i)
    for (list in readAll) {
        val title = list.get(0).toString()
        val author = removeNoZhCn(list.get(1).toString())
        val urlDetail = list.get(2).toString()
        skrape(BrowserFetcher) {
            request {
                url = "https:$urlDetail"
                headers = initHeader()
                cookies = initCookie()
                userAgent = UserAgent
                method = Method.GET
                body = null
                this.timeout = 10000
                charset(CharsetUtil.GBK)
            }
            response {
                htmlDocument {
                    ".content" {
                        findAll {
                            val p = StringBuilder()
                            forEach { it ->
                                it.children.forEach { element ->
                                    p.append(
                                        """
                                                <p>${element.text}</p>
                                            """.trimIndent()
                                    )
                                }
                            }
                            val template = """
                                            <?xml version="1.0" encoding="UTF-8"?>
                                            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                                            <html xmlns="http://www.w3.org/1999/xhtml">
                                            <head>
                                                <title>${title}</title>
                                            </head>
                                            <body>
                                                <h1>${title}</h1>
                                                $p
                                            </body>
                                            </html>
                                        """.trimIndent()
                            Console.log(template)
                            val chapter1Resource =
                                Resource(template.toByteArray(), "${System.currentTimeMillis()}.xhtml")
                            chapter1Resource.mediaType = MediatypeService.XHTML
                            resources.add(chapter1Resource)
                            book.addSection(title, chapter1Resource)
                        }
                    }
                }
            }
        }

    }
    val outputFile = File("$TangShiExcelPath/唐诗三百首.epub")
    val outputStream = FileOutputStream(outputFile)
    EpubWriter().write(book, outputStream)
    outputStream.close()
    Console.log("$TangShiExcelPath/唐诗三百首.epub")
}

fun mainx() {
    val urlDetail = "//www.diyifanwen.com/sicijianshang/tangshisanbaishou/044018104270440185187658.htm"
    val us = urlDetail.substring(2)
    Console.log(us)
}